package com.heima.wemedia.scan.listener;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.heima.common.contants.MessageConstants;
import com.heima.wemedia.scan.service.NewsScanService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;

@Configuration
public class NewsScanMessageListener {

    @Resource
    private NewsScanService newsScanService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MessageConstants.NEWS_SCAN_QUEUE, durable = "true"),
            exchange = @Exchange(value = MessageConstants.GREEN_SCAN_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = MessageConstants.NEWS_SCAN_KEY
    ))
    public void listenerNewsScanMessage(String message) throws Exception {
        if (StrUtil.isEmpty(message)) {
            return;
        }
        JSONObject jsonObject = JSONUtil.parseObj(message);
        Long newsId = jsonObject.getLong("newsId");
        String text = jsonObject.getStr("text");
        List<String> imageList = jsonObject.getJSONArray("images").toList(String.class);
        // 调用审核业务层 ,对文章文本和图片进行审核
        newsScanService.scan(newsId, text, imageList);
    }
}

